﻿/*
-----------------------------------------------------------------------------
This source file is part of Cell Cloud.

Copyright (c) 2009-2012 Cell Cloud Team - cellcloudproject@gmail.com

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
-----------------------------------------------------------------------------
*/

#ifndef CellMemoryNedPooling_h
#define CellMemoryNedPooling_h

#if CELL_MEMORY_ALLOCATOR == CELL_MEMORY_ALLOCATOR_NEDPOOLING

namespace cell
{

/** 非模板实现，仅为了隐藏 nedmalloc 细节。
*/
class _CellExport NedPoolingImpl
{
public:
	static void* allocBytes(size_t count, 
		const char* file, int line, const char* func);
	static void deallocBytes(void* ptr);
	static void* allocBytesAligned(size_t align, size_t count, 
		const char* file, int line, const char* func);
	static void deallocBytesAligned(size_t align, void* ptr);
};

/** 一个 AllocatedObject 分配策略类。这个类实际上是管理物理内存的分配与释放。
@par
	该分配策略使用的是 nedmalloc (http://nedprod.com/programs/portable/nedmalloc/index.html) 实现。
*/
class _CellExport NedPoolingPolicy
{
public:
	static inline void* allocateBytes(size_t count, 
		const char* file = 0, int line = 0, const char* func = 0)
	{
		return NedPoolingImpl::allocBytes(count, file, line, func);
	}
	static inline void deallocateBytes(void* ptr)
	{
		NedPoolingImpl::deallocBytes(ptr);
	}
	/// 返回单次分配最大尺寸。
	static inline size_t getMaxAllocationSize()
	{
		return std::numeric_limits<size_t>::max();
	}

private:
	// 不允许实例化。
	NedPoolingPolicy()
	{ }
};

/** 一个 AllocatedObject 分配策略类。这个类实际上是管理物理内存的分配与释放。
@par
	该分配策略使用的是 nedmalloc (http://nedprod.com/programs/portable/nedmalloc/index.html) 实现。
@note
	默认模板参数 Alignment 设置为 0，对齐方式依赖于具体的平台。
*/
template <size_t Alignment = 0>
class NedPoolingAlignedPolicy
{
public:
	// 编译时（compile-time）检查对齐是否可用。
	typedef int IsValidAlignment
		[Alignment <= 128 && ((Alignment & (Alignment-1)) == 0) ? +1 : -1];

	static inline void* allocateBytes(size_t count, 
		const char* file = 0, int line = 0, const char* func = 0)
	{
		return NedPoolingImpl::allocBytesAligned(Alignment, count, file, line, func);
	}

	static inline void deallocateBytes(void* ptr)
	{
		NedPoolingImpl::deallocBytesAligned(Alignment, ptr);
	}

	/// 返回单次分配最大尺寸。
	static inline size_t getMaxAllocationSize()
	{
		return std::numeric_limits<size_t>::max();
	}
private:
	// 不允许实例化。
	NedPoolingAlignedPolicy()
	{ }
};

} // end namespace cell

#endif

#endif // CellMemoryNedPooling_h
